From 1483500d1f53553fdf41dfd640b9562fd1df5424 Mon Sep 17 00:00:00 2001 From: oliskoli Date: Sat, 16 Aug 2008 13:23:03 +0000 Subject: [PATCH] Fix memory leaks in vecs/filter_vecs. --- defs.h | 2 ++ filter_vecs.c | 22 ++++++++++++++++++---- filterdefs.h | 1 + main.c | 2 ++ smplrout.c | 10 +++++----- vecs.c | 35 ++++++++++++++++++++++++++++------- 6 files changed, 56 insertions(+), 16 deletions(-) diff --git a/defs.h b/defs.h index 6f8ad4536..40932e7b5 100644 --- a/defs.h +++ b/defs.h @@ -655,6 +655,7 @@ typedef struct arglist { gbuint32 argtype; char *minvalue; /* minimum value for numeric options */ char *maxvalue; /* maximum value for numeric options */ + char *argvalptr; /* !!! internal helper. Not used in definitions !!! */ } arglist_t; typedef enum { @@ -740,6 +741,7 @@ void assign_option(const char *vecname, arglist_t *ap, const char *val); void disp_vec_options(const char *vecname, arglist_t *ap); void disp_vecs(void); void disp_vec( const char *vecname ); +void init_vecs(void); void exit_vecs(void); void disp_formats(int version); const char * name_option(long type); diff --git a/filter_vecs.c b/filter_vecs.c index d5ca9324d..f2684eac8 100644 --- a/filter_vecs.c +++ b/filter_vecs.c @@ -202,14 +202,29 @@ free_filter_vec( filter_vecs_t *fvec ) if ( fvec->args ) { for ( ap = fvec->args; ap->argstring; ap++) { - if (ap->argval && *ap->argval) { - xfree(*ap->argval); - *ap->argval = NULL; + if (ap->argvalptr) { + xfree(ap->argvalptr); + ap->argvalptr = *ap->argval = NULL; } } } } +void +init_filter_vecs(void) +{ + fl_vecs_t *vec = filter_vec_list; + while ( vec->vec ) { + arglist_t *ap; + if ( vec->vec->args ) { + for ( ap = vec->vec->args; ap->argstring; ap++ ) { + ap->argvalptr = NULL; + } + } + vec++; + } +} + void exit_filter_vecs( void ) { @@ -221,7 +236,6 @@ exit_filter_vecs( void ) vec++; } } - /* * Display the available formats in a format that's easy for humans to diff --git a/filterdefs.h b/filterdefs.h index 570450000..a7005ed63 100644 --- a/filterdefs.h +++ b/filterdefs.h @@ -42,5 +42,6 @@ void disp_filters(int version); void disp_filter( const char *vecname ); void disp_filter_vec( const char *vecname ); void disp_filter_vecs(void); +void init_filter_vecs(void); void exit_filter_vecs(void); diff --git a/main.c b/main.c index a6f9be1ab..2823e482d 100644 --- a/main.c +++ b/main.c @@ -260,6 +260,8 @@ main(int argc, char *argv[]) global_opts.inifile = inifile_init(NULL, MYNAME); } + init_vecs(); + init_filter_vecs(); cet_register(); session_init(); waypt_init(); diff --git a/smplrout.c b/smplrout.c index a7bdd2edb..0f83b0c29 100644 --- a/smplrout.c +++ b/smplrout.c @@ -61,10 +61,10 @@ static int count = 0; static double totalerror = 0; static double error = 0; -static char *countopt = NULL; -static char *erroropt = NULL; -static char *xteopt = NULL; -static char *lenopt = NULL; +static char *countopt; +static char *erroropt; +static char *xteopt; +static char *lenopt; void (*waypt_del_fnp) (route_head *rte, waypoint *wpt); static @@ -322,7 +322,7 @@ routesimple_init(const char *args) { fatal( MYNAME ": crosstrack and length may not be used together.\n"); } if ( !xteopt && !lenopt ) { - xteopt = (char *)xmalloc( 1 ); + xteopt = ""; } if (countopt) { diff --git a/vecs.c b/vecs.c index f2e2db362..315ccd86d 100644 --- a/vecs.c +++ b/vecs.c @@ -825,6 +825,22 @@ vecs_t vec_list[] = { } }; +void +init_vecs(void) +{ + vecs_t *vec = vec_list; + while ( vec->vec ) { + arglist_t *ap; + if ( vec->vec->args ) { + for ( ap = vec->vec->args; ap->argstring; ap++ ) { + ap->argvalptr = NULL; + if (ap->argval) *ap->argval = NULL; + } + } + vec++; + } +} + void exit_vecs( void ) { @@ -841,9 +857,9 @@ exit_vecs( void ) ! isdigit(ap->defaultvalue[0])) { warning("%s: not an integer\n", ap->argstring); } - if ( ap->argval && *ap->argval ) { - xfree(*ap->argval); - *ap->argval = NULL; + if ( ap->argvalptr ) { + xfree(ap->argvalptr); + *ap->argval = ap->argvalptr = NULL; } } } @@ -856,10 +872,15 @@ assign_option(const char *module, arglist_t *ap, const char *val) { char *c; - if (*ap->argval != NULL) { - xfree(*ap->argval); - *ap->argval = NULL; + if (ap->argval == NULL) + fatal("%s: No local variable defined for option \"%s\"!", module, ap->argstring); + + if (ap->argvalptr != NULL) { + xfree(ap->argvalptr); + ap->argvalptr = NULL; } + if (ap->argval) *ap->argval = NULL; + if (val == NULL) return; if (case_ignore_strcmp(val, ap->argstring) == 0) c = ""; @@ -911,7 +932,7 @@ assign_option(const char *module, arglist_t *ap, const char *val) (*c == '0') && (ap->defaultvalue == NULL)) { return; } - *ap->argval = xstrdup(c); + *ap->argval = ap->argvalptr = xstrdup(c); } void -- 2.30.2